package com.hunan.api.catv.service.biz.column;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.CacheName;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.ColumnEnum;
import com.hunan.api.catv.entity.enums.State;
import com.hunan.api.catv.entity.po.Column;
import com.hunan.api.catv.entity.vo.ColumnVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.system.SystemDictBiz;
import com.hunan.api.catv.service.impl.column.ColumnService;
import com.hunan.api.catv.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 栏目
 */
@CacheConfig(cacheNames = CacheName.column)
@Service
public class ColumnBiz extends ResultService {

    @Autowired
    ColumnService columnService;

    @Autowired
    ColumnBiz columnBiz;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    CacheBiz cacheBiz;

    @Autowired
    SystemDictBiz systemDictBiz;

    public ServiceResult add(Column column) {
        ColumnEnum columnEnum = ColumnEnum.getResult(column.getType());
        column.setUrl(columnEnum.getUrl());
        column.setTop(columnEnum.getUrl());
        return columnService.add(column);
    }

    public ServiceResult update(Column column) {
        if (ObjectUtils.isEmpty(column.getColumnId())) {
            return buildFail("", "columnId is null");
        }
        return columnService.update(column);
    }

    public ServiceResult update(Integer columnId, Integer state) {
        Column column = new Column();
        column.setColumnId(columnId);
        column.setState(state);
        return columnService.update(column);
    }

    public ColumnVo info(Integer columnId) {
        return columnService.getById(columnId);
    }

    public Integer getIdbyTitle(String title) {
        Column column = new Column();
        column.setTitle(title);
        ColumnVo columnVo = columnService.get(column);
        if (!ObjectUtils.isEmpty(columnVo)) {
            return columnVo.getColumnId();
        }
        return null;
    }

    public List<Object> typeList() {
        return ColumnEnum.columnList;
    }

    public PageInfo<ColumnVo> list(Integer type, int pageNum, int pageSize, String title) {
        PageHelper.startPage(pageNum, pageSize);
        Column column = new Column();
        column.setType(type);
        column.setTitle(title);
        List<ColumnVo> columnVoList = columnService.list(column);
        for (int i = 0; i < columnVoList.size(); i++) {
            ColumnVo columnVo = columnVoList.get(i);
            ColumnEnum columnEnum = ColumnEnum.getResult(columnVo.getType());
            columnVo.setTypeMsg(columnEnum.getValue());
            State state = State.getResult(columnVo.getState());
            columnVo.setStateMsg(state.getMsg());
        }

        return new PageInfo<>(columnVoList);
    }

    @Cacheable(unless = "#result == null")
    public List<ColumnVo> list(Integer type, String version) {
        Column column = new Column();
        column.setType(type);
        column.setState(1);
        List<ColumnVo> columnVos = columnService.simpleList(column);
        if (StringUtils.isNotBlank(version) && Integer.valueOf(version) > 199 && type == 3) {
            column.setType(4);
            columnVos = Stream.of(columnVos, columnService.simpleList(column)).flatMap(Collection::stream).collect(Collectors.toList());
        }
        return columnVos;
    }

    public ServiceResult updateSeq(Integer columnId, Integer seq) {
        Column column = new Column();
        column.setColumnId(columnId);
        column.setSeq(seq);
        ServiceResult updaterResult = columnService.update(column);
        if (updaterResult.getStatus()) {
            return buildOK(true, ConstantsMsg.admin_edit_success);
        }
        return buildFail(ConstantsMsg.admin_edit_fail);
    }
}